//
// Created by Semir on 2020/4/2.
//


#include <iostream>

using namespace std;

int const MAX_T_SIZE = 1010;
int const MAX_M_SIZE = 110;
int T, M;
int times[MAX_M_SIZE];
int value[MAX_M_SIZE];
int dp[MAX_T_SIZE] = {0};

void TakeHerb() {
    for (int i = 1; i <= M; i++) {
        for (int j = T; j >= times[i]; j--) {
            dp[j] = max(dp[j - times[i]] + value[i], dp[j]);
        }
    }
}

int main () {
    cin >> T >> M;
    for (int i = 1; i <= M; i++) {
        cin >> times[i] >> value[i];
    }
//    for (int i = 1; i <= M; i++) {
//        for (int j = T; j > 0; j--) {
//            if (j - times[i] >= 0) {
//                dp[j] = max(dp[j - times[i]] + value[i], dp[j]);
//            } else {
//                break;
//            }
//        }
//    }
    TakeHerb();
    cout << dp[T] << endl;
    return 0;
}

